package com.jinlejia.bbs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTabHost;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.logic.UserTagInfoLogic;
import com.jinlejia.bbs.ui.fun.BBSChooseTagActivity;
import com.jinlejia.bbs.ui.gambit.BBSGambitMainFragment;
import com.jinlejia.bbs.ui.message.BBSMessageFragment;
import com.jinlejia.bbs.ui.mine.BBSMineFragment;
import com.lab.Lab;

public class BBSMainActivity extends FragmentActivity {

    private int tabImg[] = {R.drawable.bbs_tab_gambit, R.drawable.bbs_tab_message, R.drawable.bbs_tab_mine};
    private String tabNames[] = {"话题", "消息", "个人"};

    private Class []tabFragments = {BBSGambitMainFragment.class, BBSMessageFragment.class, BBSMineFragment.class};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bbs_main_activity);
        Intent intent = getIntent();
        initView();
        initPush(intent);
        new UserTagInfoLogic(this).execute(new UserTagInfoLogic.LogicCallBack() {
            @Override
            public void onResult(boolean result) {
                if (result){
                    Intent intent = new Intent();
                    intent.setClass(BBSMainActivity.this, BBSChooseTagActivity.class);
                    intent.putExtra("ignore",true);
                    intent.putExtra("save",true);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initPush(intent);
    }

    private void initPush(Intent intent) {
        if (intent==null)return;
        String url = intent.getStringExtra("url");
        String code = intent.getStringExtra("code");
        Lab.bbsPushProtocol().sendBBSPush(this,url,code);

    }

    private void initView(){
        FragmentTabHost tabHost = findViewById(R.id.tabHost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.tabContent);
        for (int i = 0; i < tabNames.length; i++) {
            String name = tabNames[i];
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(name).setIndicator(getTabItemView(i));
            tabHost.addTab(tabSpec,tabFragments[i],null);
        }
    }
    private LayoutInflater layoutInflater;
    private View getTabItemView(int index){
        if (layoutInflater==null)layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.bbs_main_tab_item, null);
        ImageView imageView = view.findViewById(R.id.imageView);
        imageView.setImageResource(tabImg[index]);
        TextView textView = view.findViewById(R.id.textView);
        textView.setText(tabNames[index]);
        return view;
    }
}
